﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.iss.inventory.Controllers;
using System.Drawing;

namespace Staionery_Inventory.Presentation.Admin
{
    public partial class ItemMaintenanceUI__Reload_ : System.Web.UI.Page
    {
        ItemMaintenanceController itemCon = new ItemMaintenanceController();
        public List<String> allItemID;
        private int pointer = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            //security and current user name
            if (Session["empID"] == null)
            {
                Response.Redirect("~/Presentation/login.aspx");
            }

            Label lbl = this.Master.FindControl("lblLogInName") as Label;
            if (lbl != null)
            {
                lbl.Text = Session["UserName"].ToString();
            }

            allItemID = itemCon.getAllItemID();
            if (Session["currentPointer"] == null)
            {
                lblMessage.Text = "Click ◀ and ▶ button for sequence browsing";
                Session["currentPointer"] = 0;
                String[] item = itemCon.getItemByID(allItemID[pointer].ToString());
                setDataToTextboxes(item);
            }
            pointer = Convert.ToInt32(Session["currentPointer"]);
        }

        public void setDataToTextboxes(String[] item)
        {
            txtItemID.Text = item[0]; ddlCategory.Text = item[1]; txtDescription.Text = item[2]; txtQtyLeft.Text = item[3]; txtReorderLevel.Text = item[4]; txtReorderQty.Text = item[5]; ddlUnitofMeasure.Text = item[6]; txtLocation.Text = item[7];
        }

        public void clearFields()
        {
            txtItemID.Text = ""; ddlCategory.Text = "Choose a Category"; txtDescription.Text = ""; txtQtyLeft.Text = ""; txtReorderLevel.Text = ""; txtReorderQty.Text = ""; ddlUnitofMeasure.Text = "Choose a Unit"; txtLocation.Text = "";
        }

        protected void btnPrev_Click(object sender, EventArgs e)
        {
            lblMessage.Text = "";
            if (pointer == 0) { pointer = allItemID.Count(); }
            pointer -= 1;
            String[] department = itemCon.getItemByID(allItemID[pointer].ToString());
            Session["currentPointer"] = Convert.ToString(pointer);
            setDataToTextboxes(department);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            lblMessage.Text = "";
            if (pointer == allItemID.Count() - 1) { pointer = -1; }
            pointer += 1;
            String[] department = itemCon.getItemByID(allItemID[pointer].ToString());
            Session["currentPointer"] = Convert.ToString(pointer);
            setDataToTextboxes(department);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            lblMessage.Text = "";
            if (btnUpdate.Text == "Update")
            {
                ddlCategory.Enabled = true; txtDescription.ReadOnly = false; txtQtyLeft.ReadOnly = false; txtReorderLevel.ReadOnly = false; txtReorderQty.ReadOnly = false; ddlUnitofMeasure.Enabled = true; txtLocation.ReadOnly = false;
                btnUpdate.Text = "Save";
            }
            else if (btnUpdate.Text == "Save")
            {
                if (ddlCategory.Text == "Choose a Category")
                {
                    lblMessage.ForeColor = Color.Red;
                    lblMessage.Text = "Please choose a category";
                }
                else if (ddlUnitofMeasure.Text == "Choose a Unit")
                {
                    lblMessage.ForeColor = Color.Red;
                    lblMessage.Text = "Please choose a unit";
                }
                else
                {
                    String result = itemCon.updateItem(txtItemID.Text, ddlCategory.Text, txtDescription.Text, txtQtyLeft.Text, txtReorderLevel.Text, txtReorderQty.Text, ddlUnitofMeasure.Text, txtLocation.Text);
                    if (result == "Update Item Successfully")
                    {
                        lblMessage.ForeColor = Color.Black;
                        lblMessage.Text = result;
                        ddlCategory.Enabled = false; txtDescription.ReadOnly = true; txtQtyLeft.ReadOnly = true; txtReorderLevel.ReadOnly = true; txtReorderQty.ReadOnly = true; ddlUnitofMeasure.Enabled = false; txtLocation.ReadOnly = true;
                        btnUpdate.Text = "Update";
                        allItemID = itemCon.getAllItemID();
                    }
                    else
                    {
                        lblMessage.ForeColor = Color.Red;
                        lblMessage.Text = result;
                    }
                }
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            lblMessage.Text = "";
            String result = itemCon.deleteItem(txtItemID.Text);
            allItemID = itemCon.getAllItemID();
            if (pointer != 0) { pointer -= 1; }
            Session["currentPointer"] = pointer;
            String[] item = itemCon.getItemByID(allItemID[pointer].ToString());
            setDataToTextboxes(item);

            lblMessage.ForeColor = Color.Black;
            lblMessage.Text = result;
        }

        protected void btnCreateItem_Click(object sender, EventArgs e)
        {
            lblMessage.Text = "";
            if (btnCreateItem.Text == "New")
            {
                ddlCategory.Enabled = true; txtDescription.ReadOnly = false; txtQtyLeft.ReadOnly = false; txtReorderLevel.ReadOnly = false; txtReorderQty.ReadOnly = false; ddlUnitofMeasure.Enabled = true; txtLocation.ReadOnly = false;
                clearFields();
                btnCreateItem.Text = "Save";
            }
            else if (btnCreateItem.Text == "Save")
            {
                if (ddlCategory.Text == "Choose a Category") {
                    lblMessage.ForeColor = Color.Red;
                    lblMessage.Text = "Please choose a category";
                }
                else if (ddlUnitofMeasure.Text == "Choose a Unit")
                {
                    lblMessage.ForeColor = Color.Red;
                    lblMessage.Text = "Please choose a unit";
                }
                else {
                    String result = itemCon.createItem(ddlCategory.Text, txtDescription.Text, txtQtyLeft.Text, txtReorderLevel.Text, txtReorderQty.Text, ddlUnitofMeasure.Text, txtLocation.Text);
                    if (result == "Create Item Successfully")
                    {
                        allItemID = itemCon.getAllItemID();
                        lblMessage.ForeColor = Color.Black;
                        lblMessage.Text = result;
                        txtItemID.Text = allItemID.Last().ToString();
                        ddlCategory.Enabled = false; txtDescription.ReadOnly = true; txtQtyLeft.ReadOnly = true; txtReorderLevel.ReadOnly = true; txtReorderQty.ReadOnly = true; ddlUnitofMeasure.Enabled = false; txtLocation.ReadOnly = true;
                        btnCreateItem.Text = "New";
                        Session["currentPointer"] = allItemID.Count() - 1;
                    }
                    else
                    {
                        lblMessage.ForeColor = Color.Red;
                        lblMessage.Text = result;
                    }
                }
            }
        }

        protected void btnCreateCategory_Click(object sender, EventArgs e)
        {
            ddlCategory.Items.Add(txtCreateCategory.Text);
            txtCreateCategory.Text = "";
        }
    }
}